package org.zy.core;
 
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
 
public class MessageEncoder extends MessageToByteEncoder<Message> {
 
	public MessageEncoder() {
 
	}
 
	@Override
	protected void encode(ChannelHandlerContext ctx, Message msg, ByteBuf out) throws Exception {
		int bodyLength = 5;
		if (msg.getData() != null) {
			bodyLength += msg.getData().length;
		}
 
		out.writeInt(bodyLength);
 
		out.writeByte(msg.getType());
 
		if (msg.getData() != null) {
			out.writeBytes(msg.getData());
		}
	}
}